package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.*;
import org.itboys.mobile.entity.mysql.TianJieWalletBlock;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileWalletBlockService;
import org.itboys.mobile.service.mysql.mobile.TianJieMobileWalletService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_钱包（去链）_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/wallet/block")
public class TianJieMobileWalletBlockController extends BaseController{
    @Autowired
    private TianJieMobileWalletBlockService walletBlockService;

    @RequestMapping(value="/getWallet",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【区块链】获取我的区块链钱包")
    public void getWallet(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            TianJieWalletBlockDto walletDto=walletBlockService.getWallet(memberId);
            ResponseMessage.success(walletDto);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/wallet/block/getWallet----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    @RequestMapping(value="/getRecord",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【区块链】获取我的区块链钱包交易记录")
    public void getRecord(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(required = true, name = "type", value = "记录类型。1充值、退出记录，2买入卖出交易记录") @RequestParam(value = "type", required = true) Integer type,
                          @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                          @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                          @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                          @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            List<TianJieWalletBlockRecordDto> recordDtos=walletBlockService.getRecord(request,memberId,type);
            ResponseMessage.success(recordDtos);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/block/getRecord----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/getMarketPrice",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【区块链】获取区块链交易货币实时价格")
    public void getMarketPrice(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(required = true, name = "type", value = "交易币类型。1，默认币") @RequestParam(value = "type", required = true) Integer type,
                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            BigDecimal marketPrice = walletBlockService.getMarketPrice(memberId,type);
            ResponseMessage.success(marketPrice);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/block/getMarketPrice----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/buy",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【区块链】买入(可用金额：区块链-钱包-ableAmount字段)。（提交数据前提示：交易价格以实时成交价格为准!）")
    public void buy(HttpServletRequest request,
                            HttpServletResponse response,
                            @ApiParam(required = true, name = "blockNumber", value = "买入数量") @RequestParam(value = "blockNumber", required = true) BigDecimal blockNumber,
                            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
           //  walletBlockService.buy(memberId,blockNumber);
           // ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/block/buy----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/sale",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【区块链】卖出(可卖出数量：区块链-钱包-blockNumber字段)（提交数据前提示：交易价格以实时成交价格为准！）")
    public void sale(HttpServletRequest request,
                     HttpServletResponse response,
                     @ApiParam(required = true, name = "blockNumber", value = "卖出数量") @RequestParam(value = "blockNumber", required = true) BigDecimal blockNumber,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
          //  walletBlockService.sale(memberId,blockNumber);
          //  ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/block/sale----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/quit",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【区块链】推出区块链。注意提示用户卖出所有的持币数量")
    public void quit(HttpServletRequest request,
                     HttpServletResponse response,
                   //  @ApiParam(required = true, name = "blockNumber", value = "卖出数量") @RequestParam(value = "blockNumber", required = true) BigDecimal blockNumber,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
           // walletBlockService.quit(memberId);
          //  ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/wallet/block/quit----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 比特币交易实时行情
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/trade")
    @ApiOperation(value="【区块链】货币实时行情。网页地址：服务器+/tianjie/mobile/finance/wallet/block/trade?type=1(暂时固定参数)")
    public String trade(HttpServletRequest request,
                        Model model,
                        @ApiParam(required = true, name = "type", value = "交易币类型。1，默认币") @RequestParam(value = "type", required = true) Integer type
                        ){

        return "/tianjie/wallet/block/trade";
    }
}
